using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RemoveMine.Entities;

namespace RemoveMine.GUI
{
    public partial class frmMain : Form
    {
      
        public frmMain()
        {
            _frmChinh = this;
            InitializeComponent();
        }

        private VanChoiDTO _vanchoi;

        internal VanChoiDTO Vanchoi
        {
            get { return _vanchoi; }
            set { _vanchoi = value; }
        }
       public   DoKhoDTO _dokhodto;

        public DoKhoDTO Dokho
        {
            get { return _dokhodto; }
            set { _dokhodto = value; }
        }
        private UCOco[,] _cacUCOco;

        public UCOco[,] CacUCOco
        {
            get { return _cacUCOco; }
            set { _cacUCOco = value; }
        }
        private Timer _bodinhthoi;
        public Timer Bodinhthoi
        {
            get { return _bodinhthoi; }
            set { _bodinhthoi = value; }
        }
        private int _gio;
       

        public static frmMain _frmChinh;
        public static bool _thua;

        private void frmMain_Load(object sender, EventArgs e)
        {
            _beginMenuItem.Checked = true;
            _interMenuItem.Checked = false;
            _expertMenuItem.Checked = false;
            _bodinhthoi = new Timer();
            _bodinhthoi.Interval = 1000;
            this._bodinhthoi.Tick += new EventHandler(TimerOnTick);
            _dokhodto = new DoKhoDTO(9, 9, 10);
            KhoiTaoVanMoi(); 
        }
        private void _newMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            HuyVanCu();
            KhoiTaoVanMoi();
            Cursor.Current = Cursors.Default;
        }

        private void _beginMenuItem_Click(object sender, EventArgs e)
        {
            _beginMenuItem.Checked = true;
            _interMenuItem.Checked = false;
            _expertMenuItem.Checked = false;
            Cursor.Current = Cursors.WaitCursor;
            HuyVanCu();
            _dokhodto.Hang = 9;
            _dokhodto.Cot = 9;
            _dokhodto.Somin = 10;
            KhoiTaoVanMoi();
            Cursor.Current = Cursors.Default;
        }
        
        private void _interMenuItem_Click(object sender, EventArgs e)
        {
            _beginMenuItem.Checked = false;
            _interMenuItem.Checked = true;
            _expertMenuItem.Checked = false;
            Cursor.Current = Cursors.WaitCursor;

            HuyVanCu();
            _dokhodto.Hang = 16;
            _dokhodto.Cot = 16;
            _dokhodto.Somin = 40;
            KhoiTaoVanMoi();
            Cursor.Current = Cursors.Default;
        }

        private void _expertMenuItem_Click(object sender, EventArgs e)
        {
            _beginMenuItem.Checked = false;
            _interMenuItem.Checked = false;
            _expertMenuItem.Checked = true;
            Cursor.Current = Cursors.WaitCursor;
            HuyVanCu();
            _dokhodto.Hang = 16;
            _dokhodto.Cot = 30;
            _dokhodto.Somin = 99;
            KhoiTaoVanMoi();
            Cursor.Current = Cursors.Default;
        }
        private void _exitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       

        public  void KhoiTaoVanMoi()

        {
            int i, j;
            Bitmap _hinh; UCOco _ucoco; Point _toado;

            string _duongdan = Application.StartupPath + "\\GiaTriOCo\\" + "ChuaMo.bmp";
            _hinh = new Bitmap(Bitmap.FromFile(_duongdan));
            _vanchoi = new VanChoiDTO(_dokhodto, TTVanChoi.ChuaBatDau, _dokhodto.Somin);
            _cacUCOco = new UCOco[_dokhodto.Hang, _dokhodto.Cot];
            _gio = 0;

            _panelQuanCo.Size = new Size(_dokhodto.Cot * 17 + 2, _dokhodto.Hang * 17 + 2);
            _panelHienThi.Width = _panelQuanCo.Width;
            _panelNhom.Width = _panelQuanCo.Left * 3 + _panelQuanCo.Width;
            _panelNhom.Height = _panelQuanCo.Location.Y + _panelQuanCo.Height + 10;

            this.Height = _panelNhom.Location.Y + _panelNhom.Height + 35;
            this.Width = _panelNhom.Location.X + _panelNhom.Width + 8;

            for (i = 0; i < _dokhodto.Hang; i++)
                for (j = 0; j < _dokhodto.Cot; j++)
                {
                    _ucoco = new UCOco();
                    _ucoco.Picture = _hinh;
                    _ucoco.Oco = _vanchoi.CacOco[i, j];
                    _toado = new Point(j * 17, i * 17);
                    _ucoco.Location = _toado;
                    _cacUCOco[i, j] = _ucoco;
                    _panelQuanCo.Controls.Add(_ucoco);
                 }
             Invalidate();

        }

        public void HienThiSo(Graphics grfx, Point diem, int so)
        {
            string _duongdan, _tenfile; Image _hinh; 
            int i, _sohienthi, _tam = so, _hang = 100;
            for (i = 0; i < 3; i++)
            {
                _duongdan = Application.StartupPath + "\\DongHo\\";
                _sohienthi = _tam;
                _sohienthi /= _hang; _tam %= _hang; _hang /= 10;
                _tenfile = _sohienthi.ToString(); _tenfile += ".bmp";
                _duongdan += _tenfile;
                _hinh = Image.FromFile(_duongdan);
                grfx.DrawImage(_hinh, diem);
                diem.X += 13;
            }

        }

      

        public void HuyVanCu()

        {
            int i, j; UCOco _ucoco;
            for (i = 0; i < _dokhodto.Hang; i++)
                for (j = 0; j < _dokhodto.Cot; j++)
                {
                    _ucoco = _cacUCOco[i, j];
                    _panelQuanCo.Controls.Remove(_ucoco);
                }
            Invalidate();
        }
        public void MoDeQui(Point diemzero)
        {

            Stack<Point> _staPoint = new Stack<Point>();
            _staPoint.Push(diemzero);
            while (_staPoint.Count != 0)
            {

                int dx, dy;
                Point _diemdangxet = _staPoint.Pop();
                for (dx = -1; dx <= 1; dx++)
                    for (dy = -1; dy <= 1; dy++)
                    {
                        int _hang, _cot;
                        _hang = _diemdangxet.Y + dy; _cot = _diemdangxet.X + dx;
                        if ((_hang >= 0 && _hang < _dokhodto.Hang) && (_cot >= 0 && _cot < _dokhodto.Cot))
                        {
                            UCOco _ucocodangxet = _cacUCOco[_hang, _cot];
                            OcoDTO _ocodangxet = _ucocodangxet.Oco;
                            Point _toado = new Point(_cot, _hang);
                            string _duongdan = Application.StartupPath + "\\GiaTriOCo\\";
                            string _tenfile = "";
                            if (_ocodangxet.Hanhdong == HanhDong.ChuaMo)
                            {
                                _tenfile = _ocodangxet.Giatri.ToString();
                                _tenfile += ".bmp"; _duongdan += _tenfile;
                                _ocodangxet.Hanhdong = HanhDong.Mo;
                                _ucocodangxet.Picture = new Bitmap(Bitmap.FromFile(_duongdan));
                                _ucocodangxet.Invalidate();
                            }
                            if (_tenfile == "Zero.bmp" && !_staPoint.Contains(_toado))
                                _staPoint.Push(_toado);
                        }
                    }
            }
        }
        public bool MoHinhChuNhat(Point toado)
        {
            int dx, dy;
            bool _ketqua = false;
            for (dx = -1; dx <= 1; dx++)
                for (dy = -1; dy <= 1; dy++)
                {
                    int _hang, _cot;
                    _hang = toado.Y + dy; _cot = toado.X + dx;
                    if ((_hang >= 0 && _hang < _dokhodto.Hang) && (_cot >= 0 && _cot < _dokhodto.Cot))
                    {
                        UCOco _ucocodangxet = _cacUCOco[_hang, _cot];
                        OcoDTO _ocodangxet = _ucocodangxet.Oco;
                        Point _toado = new Point(_cot, _hang);
                        string _duongdan = Application.StartupPath + "\\GiaTriOCo\\";
                        string _tenfile = "";
                        if (_ocodangxet.Hanhdong == HanhDong.ChuaMo)
                        {
                            _tenfile = _ocodangxet.Giatri.ToString();
                            _tenfile += ".bmp"; _duongdan += _tenfile;
                            _ocodangxet.Hanhdong = HanhDong.Mo;
                            _ucocodangxet.Picture = new Bitmap(Bitmap.FromFile(_duongdan));
                            _ucocodangxet.Invalidate();
                        }
                    }
                }
            return _ketqua;
        }

        public void XuLyThua()
        {
            int i, j;

            for (i = 0; i < _dokhodto.Hang; i++)
                for (j = 0; j < _dokhodto.Cot; j++)
                {
                    string _duongdan = Application.StartupPath+"\\GiaTriOCo\\";
                    string _tenfile;
                    OcoDTO _oco;

                    _oco = _cacUCOco[i, j].Oco;
                    if (_oco.Giatri  == GiaTriOCo .Bom )                    {
                        if (_oco.Hanhdong == HanhDong.ChuaMo)
                        {
                            _tenfile = _oco.Giatri .ToString();
                            //_thua = false;
                            _tenfile += ".bmp"; _duongdan += _tenfile;
                            _cacUCOco[i, j].Picture = new Bitmap(Bitmap.FromFile(_duongdan));
                            _cacUCOco[i, j].Invalidate();
                        }
                    }
                    else
                        if (_oco.Hanhdong == HanhDong.DatCo)
                        {
                            _tenfile = "BomCheo.bmp"; _duongdan += "\\" + _tenfile;
                            _cacUCOco[i, j].Picture = new Bitmap(Bitmap.FromFile(_duongdan));
                            _cacUCOco[i, j].Invalidate();
                        }
                }
        }

        void TimerOnTick(object obj, EventArgs ea)
        {
            _gio++;
            _panelHienThi.Invalidate ();
        }            

      

        private void _panelHienThi_Paint(object sender, PaintEventArgs e)
        {
            Graphics _grfx = e.Graphics;
            HienThiSo(_grfx, new Point(_panelHienThi.Width - 10 - 3 * 13, 6), _gio);
            HienThiSo(_grfx, new Point(7, 6), _vanchoi.Socodung);
        }

        private void customToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Custom cus = new Custom();
            cus.Show();
        }

        
        

      
    }
}